package com.lanhai.javaBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.lanhai.model.Park;
import com.lanhai.utils.RegionUtil;
import com.mybatis.dao.ParkDao;

public class ParkBean {

	/**
	 * 加载Agv泊位列表
	 */
	public static List<Park> loadData(SqlSessionFactory sqlSessionFactory){
		List<Park> parkList = new ArrayList<Park>();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
			ParkDao dao = sqlSession.getMapper(ParkDao.class);
			List<Map<String, Object>> list = dao.selectAll();
			if(list.size() > 0){
				for(Map<String, Object> m:list){
					Park park = new Park();
					park.setId((Integer)m.get("id"));
					park.setName((String)m.get("name"));
					park.setSname((String)m.get("sname"));
					park.setStationName((String)m.get("stationName"));
					park.setType((Integer)m.get("type"));
					park.setSubType((Integer)m.get("subType"));
					park.setDot((Integer)m.get("dot"));
					park.setOutNum((Integer)m.get("outNum"));
					park.setIpAddress((String)m.get("ipAddress"));
					park.setPort((Integer)m.get("port"));
					park.setpGroup((Integer)m.get("pGroup"));
					park.setItem((Integer)m.get("item"));
					park.setStep((Integer)m.get("step"));
					park.setTrunNum((Integer)m.get("trunNum"));
					park.setXtIpAddress((String)m.get("xtIpAddress"));
					park.setXtPort((Integer)m.get("xtPort"));
					park.setXzdNumItem((Integer)m.get("xzdNumItem"));
	                parkList.add(park);					
				}
			}
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	sqlSession.close();
	    }
        return parkList;
	}
	
	/**
	 * 对列表以序号从大到小进行排序
	 * @param parkList
	 * @return
	 */
	public static List<Park> ItemSort(List<Park> parkList){
        Collections.sort(parkList, new Comparator<Object>() {
            public int compare(Object a, Object b) {
                int one = ((Park) a).getItem();
                int two = ((Park) b).getItem();
                return one - two;//升序
                //return two - one;//降序
            }
        });	
        return parkList;
	}
	/**
	 * 判断当前座标是否在某个站点的半径范围内
	 * @param parkList
	 * @param currentX
	 * @param currentY
	 * @param radius
	 */
	public static Park contrast(Map<Integer, Park> parkMap, double currentX, double currentY
			, double radius){
		Park p = null;
		for(Map.Entry<Integer, Park> map:parkMap.entrySet()){
			Park park = map.getValue();
			double distance = RegionUtil.distance(park.getMapX(), park.getMapY(), currentX, currentY);
			//System.out.println("minX:" + park.getMapX() + ",minY:" + park.getMapY());
			if(distance < radius){
				p = park;
				break;
			}			
		}
		return p;
	}

	public static List<Map<String, Object>> loadTempData(SqlSessionFactory sqlSessionFactory){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
			ParkDao dao = sqlSession.getMapper(ParkDao.class);
			list = dao.selectTempData();
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	sqlSession.close();
	    }
		return list;
	}
	
}
